package com.china08.yxyapi.rabbitmq.listener.schwebsit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.config.RabbitConfig;
import com.china08.yxyapi.entity.mg.sws.SwsPvForDailyAnalyze;
import com.china08.yxyapi.entity.mg.sws.SwsPvForTotalAnalyze;
import com.china08.yxyapi.repository.mg.SwsPvForDailyAnalyzeRepository;
import com.china08.yxyapi.repository.mg.SwsPvForTotalAnalizeRepository;

@Component
@EnableRabbit
public class SchoolWebsitePvListener {

    @Autowired
    private SwsPvForTotalAnalizeRepository swsPvForTotalAnalizeRepository;

    @Autowired
    private SwsPvForDailyAnalyzeRepository swsPvForDailyAnalyzeRepository;

    private static final Logger LOGGER = Logger.getLogger(SchoolWebsitePvListener.class);

    /**
     * 修改学校网站日访问量和总访问量
     * 
     * @throws ParseException
     * 
     */
    @RabbitListener(queues = RabbitConfig.YW_SCHOOL_WEBSITE_PV)
    public void schoolWebsitPvUpd(String schoolId) throws ParseException {

        SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LocalDate curDate = LocalDate.now();
        Date startDate = dFormat.parse(curDate.toString() + " 00:00:00");
        Integer yearForPv = curDate.getYear();
        Integer monthForPv = curDate.getMonthValue();
        Integer dayForPv = curDate.getDayOfMonth();
        SwsPvForDailyAnalyze pvForDailyAnalyze = swsPvForDailyAnalyzeRepository
                .findBySchoolIdAndYearForPvAndMonthForPvAndDayForPv(schoolId, yearForPv, monthForPv, dayForPv);
        if (null == pvForDailyAnalyze) {
            SwsPvForDailyAnalyze forDailyAnalyze = new SwsPvForDailyAnalyze();
            forDailyAnalyze.setPv(1l);
            Integer year = curDate.getYear();
            Integer month = curDate.getMonthValue();
            Integer day = curDate.getDayOfMonth();
            forDailyAnalyze.setDayForPv(day);
            forDailyAnalyze.setMonthForPv(month);
            forDailyAnalyze.setYearForPv(year);
            forDailyAnalyze.setDateForPv(startDate);
            forDailyAnalyze.setSchoolId(schoolId);
            swsPvForDailyAnalyzeRepository.save(forDailyAnalyze);
        } else {
            Long pv = pvForDailyAnalyze.getPv() + 1L;
            pvForDailyAnalyze.setPv(pv);
            swsPvForDailyAnalyzeRepository.save(pvForDailyAnalyze);
        }
        SwsPvForTotalAnalyze totalPv = swsPvForTotalAnalizeRepository.findBySchoolId(schoolId);

        if (null == totalPv) {
            SwsPvForTotalAnalyze totalAnalyze = new SwsPvForTotalAnalyze();
            totalAnalyze.setPv(1l);
            totalAnalyze.setSchoolId(schoolId);
            swsPvForTotalAnalizeRepository.save(totalAnalyze);
        } else {
            Long pv = totalPv.getPv() + 1L;
            totalPv.setPv(pv);
            swsPvForTotalAnalizeRepository.save(totalPv);
        }
    }
}
